<template>
  <div>
    <!-- :visible="dialogVisible"  -->
    <!-- @update:visible = "dialogVisible = $event" -->
    <!-- 如果弹出框是否关闭的变量 在父组件里面，则应该去掉sync的修饰符 -->
    <el-dialog
      :title="ruleForm.id ? '编辑部门' : '新增部门'"
      :visible="visible"
      @close="close('ruleForm')"
    >
      <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules" label-width="100px">
        <el-form-item label="部门名称" prop="name">
          <el-input v-model="ruleForm.name" />
        </el-form-item>
        <el-form-item label="部门编码" prop="code">
          <el-input v-model="ruleForm.code" />
        </el-form-item>
        <el-form-item label="负责人名称" prop="managerId">
          <el-select v-model="ruleForm.managerId" clearable class="add-dept-select" placeholder="请选择负责人">
            <el-option v-for="item in managerList" :key="item.id" :label="item.username" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="部门介绍" prop="introduce">
          <el-input v-model="ruleForm.introduce" type="textarea" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">{{ ruleForm.id ? '更新' : '确定' }}</el-button>
          <el-button @click="close('ruleForm')">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { getUserSimpleRequest, addDepartmentRequest, getDepartmentByIdRequest, updateDepartmentByIdRequest } from '@/api/department'

export default {
  name: 'AddDeptCom',
  props: {
    visible: {
      type: Boolean
    },
    originList: {
      type: Array,
      default: () => []
    },
    currentId: {
      type: Number,
      default: 0
    }
  },
  data() {
    const validatorName = async(_, values, cb) => {
      let res = []
      if (this.ruleForm.id) {
        res = this.originList.filter(item => item.id !== this.currentId)
      } else {
        res = this.originList
      }
      if (res.some(item => item.name === values)) {
        cb(new Error('部门名称重复，请重新输入'))
      } else {
        cb()
      }
    }
    const validatorCode = async(_, values, cb) => {
      let res = []
      if (this.ruleForm.id) {
        res = this.originList.filter(item => item.id !== this.currentId)
      } else {
        res = this.originList
      }
      if (res.some(item => item.code === values.trim())) {
        cb(new Error('部门编码重复，请重新输入'))
      } else {
        cb()
      }
    }

    return {
      ruleForm: {
        name: '',
        code: '',
        managerId: '',
        introduce: ''
      },
      managerList: [],
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' },
          { validator: validatorName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' },
          { validator: validatorCode, trigger: 'blur' }
        ],
        managerId: [
          { required: true, message: '部门负责人不能为空', trigger: 'change' }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { min: 1, max: 100, message: '长度在 1 到 100 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  watch: {
    currentId(newV, oldV) {
      console.log('props在watch里面更新', newV, oldV)
    }
  },
  created() {
    this.getUserSimple()
    console.log(3, this.currentId)
  },
  mounted() {
    console.log(4, this.currentId)
  },
  methods: {
    open() {
      this.dialogVisible = true
    },
    async getUserSimple() {
      const res = await getUserSimpleRequest()
      this.managerList = res.data
    },
    submitForm(formName) {
      this.$refs.ruleFormRef.validate(async(valid) => {
        if (valid) {
          if (this.ruleForm.id) {
            await updateDepartmentByIdRequest(this.ruleForm)
            this.close()
          } else {
            await addDepartmentRequest({ ...this.ruleForm, pid: this.currentId })
            this.close()
          }

          this.$emit('updateParent')
        }
      })
    },
    resetForm(formName) {
      this.$refs.ruleFormRef.resetFields()
      this.ruleForm = this.$options.data().ruleForm
      this.$emit('update:visible', false)

      console.log(333, this.$options)
    },
    close(formName) {
      this.resetForm(formName)
    },
    async getDepartmentById() {
      const res = await getDepartmentByIdRequest(this.currentId)
      this.ruleForm = res.data
    }
  }
}
</script>

<style lang="scss">

.add-dept-select {
  .el-icon-arrow-up:before {
    font-size: 30px;
  }
}
</style>

<style lang="scss" scoped>
.el-form-item .el-select {
  width: 100%;
}
.el-select .el-input .el-select__caret {
  &:before {
    font-size: 30px;
  }
}
</style>
